package com.example.forum2.controller;


import com.example.forum2.common.AppResult;
import com.example.forum2.common.ResultCode;
import com.example.forum2.congif.AppConfig;
import com.example.forum2.exception.ApplicationException;
import com.example.forum2.model.Article;
import com.example.forum2.model.Board;
import com.example.forum2.model.User;
import com.example.forum2.services.IArticleService;
import com.example.forum2.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags="文章接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService iArticleService;

    @Autowired
    private IBoardService iBoardService;
//    发布新的帖子,对三个参数进行校验
    @ApiOperation("发布新帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("板块id")@RequestParam("boardId") @NonNull Long boardId,
                            @ApiParam("文章标题")@RequestParam("title")@NonNull String title,
                            @ApiParam("文章正文")@RequestParam("content")@NonNull String content){
//      首先要判断用户是否被禁言
        HttpSession session=request.getSession(false);
//      从session中得到用户
        User user=(User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//还需要对板块进行校验
        Board board=iBoardService.selectById(boardId.longValue());
        if(board==null||board.getState()==1||board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }
//        封装文章对象
        Article article=new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setUserId(user.getId());
        article.setBoardId(boardId);
        iArticleService.create(article);
        return AppResult.success();

    }
//根据是否传入id,来显示文章信息
    @ApiOperation("获取文章列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@ApiParam("板块ID") @RequestParam(value = "boardId",required = false) Long boardId){
//定义返回的集合
        List<Article> articles;
//        判断参数是否为空
        if(boardId==null){
            articles=iArticleService.selectAll();
        }else{
            articles=iArticleService.selectAllByBoardId(boardId);
        }
//        判断结果是否为空
        if(articles==null){
            articles=new ArrayList<>();
        }
//        返回结果
        return AppResult.success(articles);
    }

    @ApiOperation("获取帖子详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails (HttpServletRequest request,
            @ApiParam("帖子ID") @RequestParam("id") @NonNull Long id){
//        从session中获取信息
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);


        Article article=iArticleService.selectDetailById(id);
//        判断结果是否为空
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);

        }
//        通过作者id和帖子表中的作者id之间的比较即知
        if(user.getId()==article.getUserId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }
//  更新帖子
    @ApiOperation("修改帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子id")@RequestParam("id")@NonNull Long id,
                            @ApiParam("帖子标题")@RequestParam("title")@NonNull String title,
                            @ApiParam("帖子正文")@RequestParam("content")@NonNull String content){

//        session用于获取当前登陆的用户,判断用户的状态,判断用户是不是帖子的作者,
          HttpSession session=request.getSession(false);
          User user=(User) session.getAttribute(AppConfig.USER_SESSION);
//校验用户的状态
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//查询帖子详情
        Article article=iArticleService.selectById(id);
        if(article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
//判断用户是否是作者
        if(user.getId()!=article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        if(article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
//调用service
        iArticleService.modify(id,title,content);
        return AppResult.success();
    }
    @ApiOperation("点赞")
    @RequestMapping("/thumbsup")
    public AppResult thumbsUp(HttpServletRequest request,
                             @ApiParam("帖子id")@RequestParam("id") @NonNull Long id){
//        首先要校验用户的状态,查看用户是否被禁言
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//        调用service,返回结果
        iArticleService.thumbsUpById(id);
//        返回结果
        return AppResult.success();
    }
    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("帖子id")@RequestParam("id")@NonNull Long id){
        HttpSession session=request.getSession(false);
        User user=(User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//        查询帖子状态
        Article article=iArticleService.selectById(id);
        if(article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
//        校验当前用户是不是帖子的作者
        if(user.getId()!=article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
//        调用service
        iArticleService.deleteById(id);
        return AppResult.success();
    }

    @ApiOperation("根据id显示用户帖子")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
            @ApiParam("用户id")@RequestParam(value = "userId", required=false) Long userId){
//        因为可以传入id,也可以不传入,判断是否需要从session中获取id
        if(userId==null){
            HttpSession session=request.getSession(false);
            User user=(User) session.getAttribute(AppConfig.USER_SESSION);
//            要从user中得到id
             userId=user.getId();
        }
//        调用service层返回即可
        List<Article> articles=iArticleService.selectByUserId(userId);
//        返回结果
        return AppResult.success(articles);
    }


}
